package com.ys.utils.redis.message;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;

/**
 * pub/sub消息队列父类
 */
@Slf4j
public abstract class TopicRedisMessageListener implements MessageListener {

    public abstract String getTopic();

    protected abstract void onMessage(RedisMessage message);

    @Override
    public void onMessage(Message message, byte[] bytes) {
        byte[] body = message.getBody();
        Object o = new JdkSerializationRedisSerializer().deserialize(body);
        if (!(o instanceof RedisMessage)) {
            log.error("错误的redis消息类型！redis消息只支持com.swing.configrer.other.RedisMessageListener.RedisMessage类型！");
            return;
        }
        System.err.println("收到订阅消息："+JSON.toJSONString(o));
        this.onMessage((RedisMessage) o);
    }

}
